<?php

class Basesf2s3iGuardActions extends autosf2s3iGuardActions
{

  public function executeList()
  {
    $this->processSort();

    $this->processFilters();


    // pager
    $this->pager = new sfPropelPager('GuardUser', 10);
    $c = new Criteria();


    if (sfConfig::get('user_display_list') == 'one'
    and !$this->getUser()->hasCredential(sfConfig::get('super_name'))
    )
    {
      $c->add(GuardUserPeer::LOGIN, $this->getUser()->getAttribute('user_login'));
    }

    $this->addSortCriteria($c);
    $this->addFiltersCriteria($c);
    $this->pager->setCriteria($c);
    $this->pager->setPage($this->getRequestParameter('page', 1));
    $this->pager->init();
  }

  public function executeGuardEdit()
  {
    $user = GuardUserPeer::retrieveByPk($this->getRequestParameter('id'));

    if ($this->getUser()->hasCredential(sfConfig::get('super_name'))
    or $this->getUser()->getAttribute('user_login') ==  $user->getLogin())
    {
      $this->redirect('sf2s3iGuard/edit?id='.$user->getId());
    }
    else
    {
      $this->redirect('sf2s3iGuard/list');
    }
  }

  public function executeGuardDelete()
  {
    $user = GuardUserPeer::retrieveByPk($this->getRequestParameter('id'));

    if ($this->getUser()->getAttribute('user_login') ==  $user->getLogin())
    {
      $this->redirect('sf2s3iGuard/list');
    }
    else
    {
      $this->redirect('sf2s3iGuard/delete?id='.$user->getId());
    }
  }

  protected function updateGuardUserFromRequest()
  {
    $guard_user = $this->getRequestParameter('guard_user');

    if (isset($guard_user['login']))
    {
      $this->guard_user->setLogin($guard_user['login']);
    }
    if (isset($guard_user['email']))
    {
      $this->guard_user->setEmail($guard_user['email']);
    }
    if (isset($guard_user['password']) and preg_match('/(.+)/', $guard_user['password']))
    {
      $this->guard_user->setPassword($guard_user['password']);
    }
    if (isset($guard_user['guard_user_permissions']))
    {
      $this->guard_user->setGuardUserPermissions($guard_user['guard_user_permissions']);
    }
  }

}
